<?php
namespace backend\controllers;

use common\models\AdminUser;
use common\models\NewsColumnType;
use common\models\SpecialColumnType;
use common\service\PublicFunction;
use Yii;


/**
 * Admin controller
 */
class AdminController extends BaseApiController
{

    /**
     * 管理员登陆
     * username 用户名或邮箱
     * password 密码
     * token  token
     * @return string
     */
    public function actionLogin()
    {
        $username = isset($this->params['username']) ? $this->params['username'] : '';
        $pwd      = isset($this->params['password']) ? $this->params['password']:'';
        $token    = isset($this->params['token']) ? $this->params['token']:'';
        $username = trim($username);
        $pwd      = trim($pwd);
        if(empty($username) || empty($pwd) || empty($token)){
            $this->_errorData("0101", "参数错误");
        }

        if($token){
            if (empty($username)) {
                $this->_errorData("0010", "请传入用户名或邮箱");
            }
            if (empty($pwd)) {
                $this->_errorData("0020", "请传入密码");
            }
            //校验密码
            $admin = AdminUser::getUserByLogin($username);

            /* 提示 未注册 */
            if(!$admin){
                $this->_errorData("0004", "该用户不存在");
            }

            //用户状态
            $status=$admin->status;
            if($status=='0'){
                $this->_errorData("0003", "用户已被禁用");
            }else{
                if ($admin && AdminUser::sp_password($pwd) == $admin->admin_pwd) {
                    $ReturnData['admin_id'] = $admin->admin_id . "";
                    $ReturnData['token'] = $token . "";
                    $ReturnData['user_name'] = empty($admin->username) ? "" : $this->userTextDecode($admin->username . "");
                    $ReturnData['real_name'] = empty($admin->real_name) ? "" : $admin->real_name . "";
                    $ReturnData['user_url']  = empty($admin->user_url) ? "" : $admin->user_url . "";

                    $admin->last_login_time = date("Y-m-d H:i:s");

                    if($admin->save()){
                        $redis = Yii::$app->cache;
                        $redis_info = $redis->get(array($admin->admin_id));
                        $redis->delete(array($redis_info['token']));
                        //设置用户其他token失效、设置有效期30天
                        $expires = AdminUser::setAccessToken($redis_info['token'], $admin->admin_id,$token);
                        PublicFunction::SetRedis($admin->admin_id,array("token"=>$token,"AdminId" => $admin->admin_id));

                        if($expires) $ReturnData['expires'] = $expires;
                        // token有效期30天
                        PublicFunction::SetRedis($token,array("AdminId" => $admin->admin_id));
                    }
                    $this->_successData($ReturnData, "登录成功");
                }else{
                    $this->_errorData("0002", "用户名或密码错误");
                }
            }
        }else{
            $this->_errorData("0002", "用户名或密码错误");
        }

    }



}

